package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.MarketMapper;
import cn.itcast.erp.entity.Market;
import cn.itcast.erp.service.MarketService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MarketService
 * @author 周昌洋
 * @date 2019/6/6
 */

//销售统计表
@Service("marketService")
public class MarketServiceImpl implements MarketService {

    @Resource
    private MarketMapper marketMapper;

    @Override
    public List<Map<String,Object>> typeMarket(Market market) {

        List<Market> marketList = this.marketMapper.typeMarket(market);

       List<Map<String,Object>> mapList = new ArrayList<>();
        for (Market mrk : marketList) {
            Map map = new HashMap();
            map.put("name",mrk.getGoodsType());
            map.put("y",mrk.getMarketMoney());

            mapList.add(map);
        }

        return mapList;
    }

    @Override
    public List<Map> yearMarket() {

        List<Market> markets = this.marketMapper.yearMarket();

        List<Map> list = new ArrayList();

        for (Market market : markets) {
            Map map = new HashMap();
            map.put("year",market.getYear());

            list.add(map);

        }
        return list;
    }

    //销售趋势表
    @Override
    public List<Map<String,Object>> monthMaket(Market market) {
        List<Market> list = this.marketMapper.monthMarket(market);
        List<Map<String,Object>> mapList = new ArrayList<>();
        int exist = 1;
        for (int i = 1; i <=12 ; i++) {
            for (Market market1 : list) {

                if ((market1.getMonth()).equals(i+"")){
                    Map map = new HashMap();
                    map.put("name",market1.getMonth());
                    map.put("y",market1.getMarketMoney());
                    mapList.add(map);
                    exist = 1;
                    break;
                }else {
                    exist = 0;
                }
            }
            if (exist == 0){
                Map map = new HashMap();
                map.put("name",i);
                map.put("y",0);
                mapList.add(map);
            }

        }

        return mapList;
    }
}
